/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package myftp.app.view;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTree;
import javax.swing.plaf.basic.BasicButtonUI;
import myftp.app.model.ServerModel;
import myftp.app.view.listener.DisconnectActionListener;
import myftp.core.explorer.FtpExplorer;
import myftp.core.explorer.LocalExplorer;
import myftp.core.view.AbstractView;

/**
 *
 * @author CAVARLE Glenn, LUCAS Stéphane
 * Vue des explorateurs (local et disant)
 */
public class ServerTabPartView extends AbstractView {

    /**
     * Titre de l'onglet
     * @var title
     */
    JLabel title = new JLabel();
    /**
     * Bouton de fermeture de l'onglet
     * @var closeButton
     */
    TabButton closeButton;
    /**
     * Explorateur Local
     * @var localExp
     */
    LocalExplorer localExp = null;
    /**
     * Explorateur Distant
     * @var serverExp
     */
    FtpExplorer serverExp = null;

    public ServerTabPartView init() {

        this.title.setText(((ServerModel) this.getModel()).getInetAddress().getHostName());
        add(this.title);
        this.title.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 5));
        this.closeButton = new TabButton();

        this.setLayout(new GridLayout(1, 2));
        this.setPreferredSize(new Dimension(980, 460));

        /* Initialisation de la partie Locale */
        this.buildLocalPart();
        /* Initialisation de la partie Distante */
        this.buildServerPart();


        /* Création des différents éléments de la vue */
        JScrollPane scrollPaneVG = new JScrollPane(localExp.getTable());
        localExp.getTable().setFillsViewportHeight(true);
        scrollPaneVG.setVerticalScrollBarPolicy(
                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        JScrollPane scrollPaneVD = new JScrollPane(serverExp.getTable());
        serverExp.getTable().setFillsViewportHeight(true);
        JSplitPane splitVG =
                new JSplitPane(JSplitPane.VERTICAL_SPLIT, new JScrollPane(localExp.getTree()), scrollPaneVG);
        splitVG.setPreferredSize(new Dimension(500, 200));
        splitVG.setDividerLocation(130);

        JSplitPane splitVD =
                new JSplitPane(JSplitPane.VERTICAL_SPLIT, new JScrollPane(serverExp.getTree()), scrollPaneVD);
        splitVD.setPreferredSize(new Dimension(500, 200));
        splitVD.setDividerLocation(130);

        JSplitPane splitH =
                new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, splitVG, splitVD);
        splitH.setSize(1000, 200);
        splitH.setDividerLocation(0.5);
        this.add(splitH);
        return this;
    }

    /**
     * Bouton de fermeture de l'onglet
     */
    private class TabButton extends JButton {

        public TabButton() {
            int size = 17;
            setPreferredSize(new Dimension(size, size));
            setToolTipText("Fermer l'onglet");
            setUI(new BasicButtonUI());
            setFocusable(false);
            setBorder(BorderFactory.createEtchedBorder());
            setBorderPainted(false);
            setRolloverEnabled(true);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g.create();
            if (getModel().isPressed()) {
                g2.translate(1, 1);
            }
            g2.setStroke(new BasicStroke(2));
            g2.setColor(Color.BLACK);
            if (getModel().isRollover()) {
                g2.setColor(Color.MAGENTA);
            }
            int delta = 6;
            g2.drawLine(delta, delta, getWidth() - delta - 1, getHeight() - delta - 1);
            g2.drawLine(getWidth() - delta - 1, delta, delta, getHeight() - delta - 1);
            g2.dispose();
        }
    }

    private void buildLocalPart() {

        /* Récupération du chemin du dossier racine de l'utilisateur */
        String currentDir = System.getProperty("user.home");
        /* Création de l'explorateur local */
        this.localExp = new LocalExplorer(currentDir + "/", this);
    }

    public void buildServerPart() {
        /* Création de l'explorateur distant */
        this.serverExp = new FtpExplorer((ServerModel) this.getModel(), this);
    }

    public JComponent getTabTitle() {
        JPanel tabtitle = new JPanel();
        closeButton.setToolTipText("Fermer l'onglet");
        closeButton.putClientProperty("serverIndex", ((ServerModel) this.getModel()).getConnectIndex());
        closeButton.addActionListener(new DisconnectActionListener());
        tabtitle.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
        tabtitle.add(this.title);
        tabtitle.add(closeButton);
        return tabtitle;
    }

    public String getTabName() {
        return this.title.getText();
    }

    public void updateUI() {
    }

    public JTree getLocalTree() {
        return localExp.getTree();
    }

    public JTree getServerTree() {
        return serverExp.getTree();
    }
}
